/**
 * Concord CRM - https://www.concordcrm.com
 *
 * @version   1.3.4
 *
 * @link      Releases - https://www.concordcrm.com/releases
 * @link      Terms Of Service - https://www.concordcrm.com/terms
 *
 * @copyright Copyright (c) 2022-2023 KONKORD DIGITAL
 */
import { createStore } from 'vuex'
import findIndex from 'lodash/findIndex'

export default createStore({
  state: {
    menu: [],
    sidebarOpen: false,
    settings: {},
  },
  mutations: {
    /**
     * Set the application settings in store
     *
     * @param {Object} state
     * @param {Object} settings
     */
    SET_SETTINGS(state, settings) {
      Object.keys(settings).forEach(settingKey => {
        state.settings[settingKey] = settings[settingKey]
      })
    },

    /**
     * Toggle the sidebar visibility
     */
    SET_SIDEBAR_OPEN(state, value) {
      state.sidebarOpen = value
    },

    /**
     * Set available menu items
     */
    SET_MENU(state, menu) {
      state.menu = menu
    },

    /**
     * Update menu item
     */
    UPDATE_MENU_ITEM(state, data) {
      const index = findIndex(state.menu, ['id', data.id])
      state.menu[index] = Object.assign({}, state.menu[index], data.data)
    },
  },
  getters: {
    /**
     * Get single menu item by given id
     */
    getMenuItem: state => id => {
      return state.menu[findIndex(state.menu, ['id', id])]
    },
  },
  modules: {},
  strict: process.env.NODE_ENV !== 'production',
})
